<?php

namespace app\admin\controller\microend;

use app\common\controller\Backend;
use think\Db;

/**
 * 提现记录管理
 *
 * @icon fa fa-circle-o
 */
class UserCash extends Backend
{

    /**
     * Cash模型对象
     * @var \app\common\model\user\Cash
     */
    protected $model = null;

    protected $user_model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\common\model\user\Cash;
        $this->user_model = new \app\common\model\User;

    }

    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField'))
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $_where["status"] = 1;
            $total = $this->model
                ->where($where)
                //->where($_where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->with("user")
                ->field("*,cash_id as id")
                ->where($where)
                //->where($_where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 改变状态
     *
     *
     */
    public function update_status(){

        if ($this->request->isPost()) {
            $params = $this->request->post();
            if ($params)
            {
                $user_this = Db::name("user_cash")->where(["cash_id"=>$params["row"]["cash_id"]])->find();
                if($user_this["status"]==$params["type"]){
                    if($user_this["status"]==2){
                        renderJson("已提现",0);
                    }else{
                        renderJson("已驳回",0);
                    }
                }
                Db::startTrans();
                try{

                    $op = $this->model->save(["status"=>$params["type"]],["cash_id"=>$params["row"]["cash_id"]]);

                    if($op){
                        if($params["type"]==2) {
                            Db::name("user")->where(["uid" => $params["row"]["uid"]])->setInc("cashed", floatval($user_this["cash"]));
                            $fine_info = [
                                "on_time"=>time(),
                                "fina_type"=>"cash",
                                "obj_type"=>"cash",
                                "obj_id"=>$user_this["cash_id"],
                                "uid"=>$user_this["uid"],
                                "username"=>$user_this["username"],
                                "fina_cash"=>$user_this["cash"],
                                "fina_channel"=>1,
                                "balance"=>$user_this["balance"],
                                "source"=>"paike"
                            ];
                            Db::name("finance")->insert($fine_info);
                        }
                        if($params["type"]==3){
                            Db::name("user")->where(["uid" => $params["row"]["uid"]])->setInc("balance", floatval($user_this["cash"]));
                            Db::name("user_cash")->where(["cash_id"=>$params["row"]["cash_id"]])->delete();
                        }
                        Db::commit();
                        renderJson("成功:".json_encode($params), 1);
                    }else{
                        renderJson("失败", 0);
                    }

                }catch (\Exception $e){
                    echo $e->getMessage();exit;
                    renderJson("失败", 0);
                }

            }
        }
    }



    /**
     *
     * 批量驳回
     *
     */
    public function mult_no()
    {


        $pass = input("post.");

        $ids = [];

        if($pass["ids"]) {

            foreach ($pass["ids"] as $k => $v) {
                $ids[] = $v["id"];
            }
            Db::name("user_cash")->where("cash_id", "in", $ids)->update(["status" => 3]);
            renderJson("成功禁用", 1);
        }else{

            renderJson("请勾选你的选项", 0);
        }

    }


    /**
     *
     * 批量通过
     */
    public function mult_pass()
    {
        $pass = input("post.");

        $ids = [];

        if($pass["ids"]) {

            foreach ($pass["ids"] as $k => $v) {
                $ids[] = $v["id"];
            }
            Db::name("user_cash")->where("cash_id", "in", $ids)->update(["status" => 2]);
            renderJson("成功通过", 1);
        }else{

            renderJson("请勾选你的选项", 0);
        }

    }


    /**
     * 测试
     */
    public function test(){

        echo numTrmb(109.1);
    }


}
